package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.AnnualPara;
import com.cfets.annualAssessment.mapper.AnnualInformMapper;
import com.cfets.annualAssessment.mapper.AnnualParaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import tk.mybatis.mapper.entity.Example;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ShenYiChou on 2017/1/23.
 */
@Service
public class AnnualParaService {
    @Autowired
    AnnualParaMapper annualParaMapper;
    @Autowired
    AnnualInformMapper annualInformMapper;

    public List<AnnualPara> getAnnualParaPage(AnnualPara annualPara) {
        Page<?> page = PageHelper.startPage(annualPara.getPage(), annualPara.getRows());
        List<AnnualPara> annualParas=null;
        Example example=new Example(AnnualPara.class);
        example.setOrderByClause("start_date DESC");
        Example.Criteria criteria =example.createCriteria();
        //from
        if(!StringUtils.isEmpty(annualPara.getStart_date())){
            criteria.andGreaterThan("end_date",annualPara.getStart_date());
        }
        //to
        if(!StringUtils.isEmpty(annualPara.getEnd_date())){
            criteria.andLessThanOrEqualTo("start_date",annualPara.getEnd_date());
        }
        if(!StringUtils.isEmpty(annualPara.getAnnual_name())){
            criteria.andEqualTo("annual_name",annualPara.getAnnual_name());
        }
        annualParas = annualParaMapper.selectByExample(example);
        if (!annualParas.isEmpty()) {
            conAnnualInform(annualParas.get(0));
        }
        annualPara.setTotalCount(page.getTotal());
        return annualParas;
    }
    //判断是否在年检时段
    public List<AnnualPara> isDuringAnnual(){
        List<AnnualPara> annualParas=null;
        Example example=new Example(AnnualPara.class);
        Example.Criteria criteria =example.createCriteria();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String str=sdf.format(date);
        criteria.andGreaterThanOrEqualTo("start_date",str);
        criteria.andLessThanOrEqualTo("end_date",str);
        return annualParaMapper.selectByExample(example);
    }

    public boolean newAnnualPara(AnnualPara annualPara) {
        return annualParaMapper.insertSelective(annualPara)>0;
    }

    public boolean updateAnnualPara(AnnualPara annualPara) {
        return annualParaMapper.updateByPrimaryKey(annualPara)>0;
    }

    public boolean deleteAnnualParaById(int id) {
        return annualParaMapper.deleteByPrimaryKey(id)>0;
    }
    //用于年检结束时保存统计信息
    public boolean saveAnnualParaStatistics(AnnualPara annualPara){
        conAnnualInform(annualPara);
        return annualParaMapper.updateByPrimaryKey(annualPara)>0;
    }

    public int[] conCertStat(){
        int[] certStat = new int[4];
        certStat[0] = annualInformMapper.conCertByStat("1");
        certStat[1] = annualInformMapper.conCertByStat("2");
        certStat[2] = annualInformMapper.conCertByStat("3");
        certStat[3] = annualInformMapper.conTotalCert();
        return certStat;
    }
    //统计年检信息 1.输入为正在年检的年检参数  2.输出为输入增加正在年检的统计信息
    public boolean conAnnualInform(AnnualPara annualPara){
        annualPara.setT_total(annualInformMapper.conTotalAssessing());
        annualPara.setT_no_annual(annualInformMapper.conAnnualAssessing("1"));
        annualPara.setT_no_check(annualInformMapper.conAnnualAssessing("2"));
        annualPara.setT_pass(annualInformMapper.conAnnualAssessing("3"));
        annualPara.setT_no_pass(annualInformMapper.conAnnualAssessing("4"));
        return true;
    }
    //判断新增年检参数是否有效
    public boolean annualParaIsValid(AnnualPara annualPara){
        List<AnnualPara> annualParas=null;
        Example example=new Example(AnnualPara.class);
        example.setOrderByClause("start_date DESC");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(annualPara.getStart_date())){
            criteria.andGreaterThan("start_date",annualPara.getStart_date());
        }
        if(!StringUtils.isEmpty(annualPara.getEnd_date())){
            criteria.andGreaterThan("end_date",annualPara.getEnd_date());
        }
        annualParas = annualParaMapper.selectByExample(example);
        if(annualParas.isEmpty()){
            return true;
        }
        return false;
    }
}
